<?php
class user
{
	
	function __autoload($className)
	{
    	require_once('/class/' . $className . '.class.php');
	}
	public static function GeneralInvite()
	{
		$RandCode=rand(10000,10000000);
		$Check=self::checkpermission('checkregis',"bath=$RandCode", '1=1');
		if($Check==0)
		{
			return $RandCode;
		}
		else 
		{
			self::generalinvite();
		}
	}
	public function __construct($user,$pass,$secretkey="tuyennguyen123")
	{
		$this->_user=$user;
		$this->_pass=$pass;
		$this->_srkey=$secretkey;
	}
	public static function checktime($ccid)
	{
		$sql="select hour(timediff(localtime(),Timeorder)) from ccinfo where ccid=$ccid";
		$Result=database::GetQueryResult($sql,0);
		$Hours=$Result[0]['hour(timediff(localtime(),timeorder))'];
		if($Hours>0)
		{
			return 0; // fail
		}
		else if($Hours==0)
		{
			return 1; // done
		}
		else 
		{
			return 3; 
		}
	}
	public static function insertorder($userid,$money,$site,$type)
	{	
		$GetOrderId=database::LimitQuery('orders',array(
		'select' => 'max(orderid)',
		'condition' => array(
		'userid' => $userid
		)
		));
		$orderidtemp=$GetOrderId[0]['max(orderid)'];
		$orderid=$orderidtemp+1;
		$date=date("Y-m-d G:i:s");
		$Insert=database::Insert('orders', array(
		'userid' => $userid,
		'orderid' => $orderid,
		'timeorder' => $date,
		'site' => $site,
		'money' => $money,
		'checkprice' => 0,
		'refund' => 0,
		'type' => $type
		));
		return $orderid;
	}
	public static function checkpermission($table,$column1,$column2)
	{
		$sql="select * from $table where $column1 and $column2";
		//echo $sql;
		$query=database::GetQueryResult($sql);
		if(empty($query))
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	public static function need_CanBuy($UserId)
	{
		$GetCanBuy=self::checkpermission('userinfo', "userid=$UserId", "can_buy =1");
		if($GetCanBuy==1)
		{
			return TRUE;
		}
		else 
		{
			return FALSE;
		}
		
	}
	public static function getmoneytemp($userid)
	{
		$sql="select amount from moneytemp where userid=$userid";
		$query=database::GetQueryResult($sql);
		if(empty($query))
		{
			return 0; //
		}
		else 
		{
			return $query['amount'];
		}
		//
	}
	public static function checklogin($user,$pass)
	{
		$pass=hash("SHA256" , $pass) ;
		$user=strval($user);
		$condition=array(
		'username' => $user,
		'password' => $pass
		);
		$option=array(
		'condition' => $condition,
		'select' => 'userid,permission'
		);
		
		$check=database::LimitQuery('userinfo',$option);
		if(empty($check))
		{
			return 0; // kiem tra khong thay co user nay
		}
		else
		{
			return $check[0];  // tra ve array userid va permission

		}
		
		
		
	}
	public function checkusername()
	{
		$condition=array(
		'username' => $this->_user
		);
		$option=array(
		'condition' => $condition,
		'select' => 'email'
		
		);
		$check=database::LimitQuery('tbluser',$option);
		if(empty($check))
		{
			return 1; // khong co user nay trong database
		}
		else
		{
			return $check['email'];
		}
		
		
	}
	public function insertuser($user,$pass,$email)
	{
		$key=$this->_pass;
		$pass=hash("SHA256" , $key) ;
		$condition=array(
		'UserName' => $user,
		'Password' => $pass,
		'email' => $email,
		'money' => '0',
		'isactive' => '1',
		'can_buy' => 1,
		'permission' => 3
		);
		$insert=database::Insert('userinfo', $condition);
		return $insert;
	}

	public function checkpw($password)
	{
		$key=$password;
		$pass=hash("SHA256" , $key) ;
		$userid=session::getuserid();
		$condition=array(
		'userid' => $userid,
		'password' => $pass,
		);
		$option=array(
		'condition' => $condition,
		'select' => 'userid'
		);
		$check=database::LimitQuery('userinfo',$option);
		if(empty($check))
		{
			return 0; // user va pass k dung
		}
		else
		{
			return 1; // user va pass dug
		}
		
	}
	
	public function gethashpw($password)
	{
		$key=$password;
		$pass=hash("SHA256" , $key) ;
		return $pass;
	}
	public static function searchAgentCountry($plan)
	{
		$plan=strval($plan);
		if(strlen($plan)<10)
		{
			$getAgentCountry=database::LimitQuery("country",array(
			'select' => 'country_code,country_name',
			'condition' => array(
			'agent_class' => $plan
			)
			));
			if(empty($getAgentCountry))
			{
				echo "Empty";
				return 0;
			}
			else 
			{
				$returnCountry=NULL;
				foreach($getAgentCountry as $listCountry)
				{
					$countryCode=$listCountry['country_code'];
					$countryName=$listCountry['country_name'];
					$returnCountry='<option value="'.$countryCode.'">'.$countryName.'</option>'.$returnCountry;
				}
				return $returnCountry;
			}
			
		}
		else
		{
			echo "Empty";
			return 0; //
		}
	}
}

?>